﻿Imports System.IO


Public Class Form_Main


    Protected FilePath As New List(Of [String])()


    Private Sub Form_Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        '// 获取当前登录用户名
        Dim UserName As String = Environment.UserName


        Dim ImageList1 As New ImageList() '// 创建图片集合
        ImageList1.ImageSize = New Size(256, 256) '// 设置图片显示大小
        ImageList1.ColorDepth = ColorDepth.Depth32Bit '// 设置位图深度


        '// 设定壁纸路径
        Dim Path As String = "C:\Users\" & UserName & "\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"
        '// 文件夹路径
        For Each File As String In Directory.GetFiles(Path, "*", SearchOption.AllDirectories)

            '// 将所有图片加入集合
            ImageList1.Images.Add(Image.FromFile(File))

            '// 输出所有文件路径
            FilePath.Add(File)

        Next


        '// 将获取到的图片写入列表
        Dim i As Integer = 0
        While i < ImageList1.Images.Count

            Dim CeateDate As DateTime = File.GetCreationTime(FilePath(i).ToString)

            Dim T1_UserPanel As New UserPanel()
            T1_UserPanel.Name = i  '// "UserPanel" &
            T1_UserPanel.Margin = New Padding(18, 18, 0, 0)
            T1_UserPanel.Size = New Size(222, 222)
            T1_UserPanel.T3_Font_Text = CeateDate.ToString()
            T1_UserPanel.T3_Font_Path = FilePath(i).ToString
            T1_UserPanel.T3_User_Image = ImageList1.Images(i)

            ' 绑定按钮的事件处理程序
            AddHandler T1_UserPanel.T3_Click, AddressOf UserPanelS_T3_Click
            FlowPane.Controls.Add(T1_UserPanel)

            i += 1
        End While



    End Sub



    ''' <summary>
    ''' 保存按钮
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click

        Dim T1_Button As System.Windows.Forms.Button = DirectCast(sender, System.Windows.Forms.Button)

        '// 创建保存路径变量
        Dim SavePath = String.Empty

        '// 创建FolderBrowserDialog对象
        Dim Fbd As New FolderBrowserDialog
        Fbd.Description = "请选择保存位置:"
        Fbd.RootFolder = Environment.SpecialFolder.Desktop  '// 默认保存桌面

        If Fbd.ShowDialog = DialogResult.OK Then
            '// 获取选中的文件夹路径
            SavePath = Fbd.SelectedPath
        End If

        Select Case T1_Button.Name.ToString
            Case "Button1"
                '// 全部保存
                For Each T1_Control As Control In FlowPane.Controls
                    If TypeOf T1_Control Is UserPanel Then
                        Dim Button As UserPanel = CType(T1_Control, UserPanel)

                        '// 获取文件名
                        Dim NewFile = Path.GetFileName(Button.T3_Font_Path)

                        '// 复制文件到指定位置
                        File.Copy(Button.T3_Font_Path, SavePath & "\" & NewFile & ".jpg")

                    End If
                Next


            Case "Button2"

                '// 保存选中
                For Each T1_Control As Control In FlowPane.Controls
                    If TypeOf T1_Control Is UserPanel Then
                        Dim Button As UserPanel = CType(T1_Control, UserPanel)
                        If Button.T3_User_Check = True Then

                            '// 获取文件名
                            Dim NewFile = Path.GetFileName(Button.T3_Font_Path)

                            '// 复制文件到指定位置
                            File.Copy(Button.T3_Font_Path, SavePath & "\" & NewFile & ".jpg")

                        End If
                    End If
                Next

        End Select

    End Sub



    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        '// 反选
        For Each T1_Control As Control In FlowPane.Controls
            If TypeOf T1_Control Is UserPanel Then
                Dim Button As UserPanel = CType(T1_Control, UserPanel)
                If Button.T3_User_Check = True Then
                    Button.T3_User_Check = False
                Else
                    Button.T3_User_Check = True
                End If

            End If
        Next

    End Sub



    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        '// 全选
        For Each T1_Control As Control In FlowPane.Controls
            If TypeOf T1_Control Is UserPanel Then
                Dim Button As UserPanel = CType(T1_Control, UserPanel)
                Button.T3_User_Check = True
            End If
        Next

    End Sub


    '// 查看图片
    Private Sub UserPanelS_T3_Click(sender As Object, e As EventArgs)

        Dim T1_Button As UserPanel = CType(sender, UserPanel)

        Dim FormView As New Form_View
        FormView.BackgroundImage = Image.FromFile(FilePath(T1_Button.Name.ToString).ToString)
        FormView.ShowDialog()


    End Sub



End Class
